#!/bin/bash


#DIR="tmp/20240927/dark_and_bias"
DIR="tmp/20240928/dark_and_bias"

if [ -d ${DIR} ]; then
    echo "> ${DIR} already exist."
else
    mkdir -p ${DIR}
fi

LOG_FILE="${DIR}/auto_dark_bias.log"

if [ -f ${LOG_FILE} ];then
    echo "--> ${LOG_FILE} exits."
else
    touch ${LOG_FILE}
fi

echo "# START of BIAS-FIELD LOG" >> ${LOG_FILE}
echo $(date) >> ${LOG_FILE}

# 曝光命令
CMD_EXPOSURE='./exposure'

# 保存DDR中数据到本地（行数、列数需要与CCD读出时序相匹配）
N_ROW=4700
N_COL=1250
N_BYTES=2
FAST_MODE=0


N_MIN=1
N_MAX=5


n=${N_MIN}
m=1
while [ ${n} -le ${N_MAX} ]
do
    FOUT_FNAME=${DIR}/bias_${m}.bin
    bin/ccd290_readout -b ${N_BYTES} -r ${N_ROW} -c ${N_COL} -f ${FAST_MODE} -o ${FOUT_FNAME}
    echo `date` >> ${LOG_FILE}
	m=$[ ${m} + 1 ]

    FOUT_FNAME=${DIR}/bias_${m}.bin
    bin/ccd290_readout -b ${N_BYTES} -r ${N_ROW} -c ${N_COL} -f ${FAST_MODE} -o ${FOUT_FNAME}
    echo `date` >> ${LOG_FILE}
	m=$[ ${m} + 1 ]

    FOUT_FNAME=${DIR}/bias_${m}.bin
    bin/ccd290_readout -b ${N_BYTES} -r ${N_ROW} -c ${N_COL} -f ${FAST_MODE} -o ${FOUT_FNAME}
    echo `date` >> ${LOG_FILE}
	m=$[ ${m} + 1 ]

    sleep 1000
	echo "n = $n"
	FOUT_FNAME=${DIR}/dark_${n}.bin
    bin/ccd290_readout -b ${N_BYTES} -r ${N_ROW} -c ${N_COL} -f ${FAST_MODE} -o ${FOUT_FNAME}
    echo `date` >> ${LOG_FILE}
	n=$[ ${n} + 1 ]

    FOUT_FNAME=${DIR}/bias_${m}.bin
    bin/ccd290_readout -b ${N_BYTES} -r ${N_ROW} -c ${N_COL} -f ${FAST_MODE} -o ${FOUT_FNAME}
    echo `date` >> ${LOG_FILE}
    m=$[ ${m} + 1 ]

    FOUT_FNAME=${DIR}/bias_${m}.bin
    bin/ccd290_readout -b ${N_BYTES} -r ${N_ROW} -c ${N_COL} -f ${FAST_MODE} -o ${FOUT_FNAME}
    echo `date` >> ${LOG_FILE}
	m=$[ ${m} + 1 ]
done

echo "# END of BIAS-FIELD LOG" >> ${LOG_FILE}

